package stage1.operation.module4.chat;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

class SendThreat implements Runnable {
    Socket socket;
    PrintWriter pWriter;
    private ArrayList<Socket> socketList;

    Scanner scanner = new Scanner(System.in);

    public SendThreat(Socket socket, ArrayList<Socket> socketList) {
        super();
        this.socket = socket;
        this.socketList = socketList;
        try {
            pWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            String strMsg = socket.getRemoteSocketAddress() + ":" + scanner.nextLine();
            for (Socket clientSock : socketList) {
                PrintWriter pWriter;
                try {
                    pWriter = new PrintWriter(clientSock.getOutputStream());
                    pWriter.println(strMsg);
                    pWriter.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

